programming4us
           
 
 
Programming

jQuery 1.3 : Headline rotator (part 6)

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
11/28/2010 7:21:05 PM

Retrieving a feed from a different domain

The news feed that we've been using for our example is a local file, but we might want to retrieve a feed from another site altogether. As we saw in Chapter 6, AJAX requests cannot, as a rule, be made to a different site than the one hosting the page being viewed. There, we discussed the JSONP data format as a method for circumventing this limitation. Here, though, we'll assume we cannot modify the data source, so we need a different solution.

To allow AJAX to fetch this file, we'll use some server-side code as a proxy for the request, so that JavaScript believes the XML file is on our server even though it actually resides on a different one. We will write a short PHP script to pull the content of the news feed to our server, and relay that data to the requesting jQuery script. This script, which we'll call feed.php, can be called in the same way feed.xml was fetched previously:

$.get('news/feed.php', function(data) {
// ...
});

Inside the feed.php file, we pull in the content of the news feed from the remote site, then print the content as the output of the script.

<?php
header('Content-Type: text/xml');
print file_get_contents('http://jquery.com/blog/feed');
?>

Note here that we need to explicitly set the content type of the page to text/xml so that jQuery can fetch it and parse it as if it were a normal, static XML document.

Some web-hosting providers may not allow the use of the PHP file_get_contents() function to fetch remote files because of security concerns. In these cases, alternative solutions, such as using the cURL library, may be available. More information on this library can be found at http://wiki.dreamhost.com/CURL.


Adding a loading indicator

Pulling in a remote file like this might take some time, depending on a number of factors, so we should inform the user that loading is in progress. To do this, we'll add a loading indicator image to the page before we issue our AJAX request.

var $loadingIndicator = $('<img/>')
.attr({
'src': 'images/loading.gif',
'alt': 'Loading. Please wait.'
})
.addClass('news-wait')
.appendTo($container);

Then, as the first line of our $.get() function's success callback, we can remove the image from the page with a simple command:

$loadingIndicator.remove();

Now, when the page first loads, if there is a delay in retrieving the headline content, we'll see a loading image rather than an empty area.

This image is an animated GIF, and in a web browser will spin to signify that activity is taking place.

We can easily create new animated GIF images for use as AJAX loading indicators by using the service at http://ajaxload.info/.

Other -----------------
- jQuery 1.3 : Headline rotator (part1) - Setting up the page
- Benchmarking Current Rankingstages of SEO
- First Stages of SEO : Benchmarking Current Rankings
- First Stages of SEO : Benchmarking Current Indexing Status
- First Stages of SEO : Assessing Historical Progress
- First Stages of SEO : Determining Top Competitors
- Relevant IAM Standards and Protocols for Cloud Services (part 2)
- Relevant IAM Standards and Protocols for Cloud Services (part 1)
- Identity and Access Management : IAM Architecture and Practice
- Identity and Access Management : Why IAM?
- Identity and Access Management : Trust Boundaries and IAM
- Parallel Programming with Microsoft .Net : Parallel Tasks - The Default Task Scheduler
- Parallel Programming with Microsoft .Net : Parallel Tasks - Design Notes
- Parallel Programming with Microsoft .Net : Parallel Tasks - Anti-Patterns
- Parallel Programming with Microsoft .Net : Parallel Tasks - Variations (part 2)
- Parallel Programming with Microsoft .Net : Parallel Tasks - Variations (part 1)
- Parallel Programming with Microsoft .Net : Parallel Tasks - An Example
- Parallel Programming with Microsoft .Net : Parallel Tasks - The Basics
- jQuery 1.3 : The jQuery UI plugin library
- jQuery 1.3 : The Form plugin
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us